package com.fangqk.service.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.stereotype.Service;

@Service("htmlService")
public class HtmlServiceImpl {

	public String getHtmlTitle(String htmlUrl) {
		String htmlSource = "";
		htmlSource = getHtmlSource(htmlUrl);// 获取htmlUrl网址网页的源码
		return getTitle(htmlSource);
	}

	/**
	 * 根据网址返回网页的源码
	 * 
	 * @param htmlUrl
	 * @return
	 */
	public String getHtmlSource(String htmlUrl) {
		URL url;
		StringBuffer sb = new StringBuffer();
		try {
			url = new URL(htmlUrl);
			BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));// 读取网页全部内容
			String temp;
			while ((temp = in.readLine()) != null) {
				sb.append(temp);
			}
			in.close();
		} catch (MalformedURLException e) {
			System.out.println("你输入的URL格式有问题！请仔细输入");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	/**
	 * 从html源码(字符串)中去掉标题
	 * 
	 * @param htmlSource
	 * @return
	 */
	public String getTitle(String htmlSource) {
		List<String> list = new ArrayList<String>();
		String title = "";

		// Pattern pa = Pattern.compile("<title>.*?</title>", Pattern.CANON_EQ);也可以
		Pattern pa = Pattern.compile("<title>.*?</title>");// 源码中标题正则表达式
		Matcher ma = pa.matcher(htmlSource);
		while (ma.find())// 寻找符合el的字串
		{
			list.add(ma.group());// 将符合el的字串加入到list中
		}
		for (int i = 0; i < list.size(); i++) {
			title = title + list.get(i);
		}
		return outTag(title);
	}

	/**
	 * 去掉html源码中的标签
	 * 
	 * @param s
	 * @return
	 */
	public String outTag(String s) {
		return s.replaceAll("<.*?>", "");
	}
}